﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using TerminationCall.Core;

namespace TerminationCall.Editor.Logic {
    public class AutoborderBrushPainter : IPainter {

        private List<PaintTile> _paintTiles;

        public EditorToolManager EditorToolManager {
            private get;
            set;
        }

        public PaintTile[] PaintTiles(Point location) {

             _paintTiles = new List<PaintTile>();

            int x = location.X;
            int y = location.Y;

            Tile choosenTile = EditorToolManager.EditorTool.ChoosenTileSet.Tiles[EditorToolManager.EditorTool.ChoosenTiles.X, EditorToolManager.EditorTool.ChoosenTiles.Y];

            _paintTiles.Add(EditorToolManager.SetNullTile(location.X, location.Y));
            _paintTiles.Add(EditorToolManager.SetSingleTile(location.X, location.Y, choosenTile));

            SetTopTile(location.X, location.Y, choosenTile);            

            // LeftBottomTile
            x = location.X - 1;
            y = location.Y + 1;

            Field leftBottomField = EditorToolManager.GetSingleField(x, y);

            if (leftBottomField != null) {
                if (leftBottomField.TileField.GetTile(TileFields.MiddleTile) != choosenTile ) {
                    if (leftBottomField.TileField.GetTile(TileFields.BottomTile) != choosenTile.ChildTileField.GetTile(TileFields.BottomTile)) {
                        if (leftBottomField.TileField.GetTile(TileFields.LeftMiddleTile) != choosenTile.ChildTileField.GetTile(TileFields.LeftMiddleTile)) {
                            _paintTiles.Add(EditorToolManager.SetSingleTile(x, y, choosenTile.ChildTileField.GetTile(TileFields.LeftBottomTile), TileFields.LeftBottomTile));
                        }
                    }
                }
            }

            // RightBottomTile
            x = location.X + 1;
            y = location.Y + 1;

            Field rightBottomField = EditorToolManager.GetSingleField(x, y);

            if (rightBottomField != null) {
                if (rightBottomField.TileField.GetTile(TileFields.MiddleTile) != choosenTile ) {
                    if (rightBottomField.TileField.GetTile(TileFields.BottomTile) != choosenTile.ChildTileField.GetTile(TileFields.BottomTile)) {
                        if (rightBottomField.TileField.GetTile(TileFields.RightMiddleTile) != choosenTile.ChildTileField.GetTile(TileFields.RightMiddleTile)) {
                            _paintTiles.Add(EditorToolManager.SetSingleTile(x, y, choosenTile.ChildTileField.GetTile(TileFields.RightBottomTile), TileFields.RightBottomTile));
                        }
                    }
                }
            }

            // BottomTile
            x = location.X;
            y = location.Y + 1;

            Field bottomField = EditorToolManager.GetSingleField(x, y);

            if (bottomField != null) {
                if (bottomField.TileField.GetTile(TileFields.MiddleTile) != choosenTile) {
                    if (bottomField.TileField.GetTile(TileFields.LeftBottomTile) == choosenTile.ChildTileField.GetTile(TileFields.LeftBottomTile)) {
                        _paintTiles.Add(EditorToolManager.SetSingleTile(x, y, null, TileFields.LeftBottomTile));
                    }
                    if (bottomField.TileField.GetTile(TileFields.RightBottomTile) == choosenTile.ChildTileField.GetTile(TileFields.RightBottomTile)) {
                        _paintTiles.Add(EditorToolManager.SetSingleTile(x, y, null, TileFields.RightBottomTile));
                    }
                    _paintTiles.Add(EditorToolManager.SetSingleTile(x, y, choosenTile.ChildTileField.GetTile(TileFields.BottomTile), TileFields.BottomTile));
                }
            }

            // LeftMiddleTile
            x = location.X - 1;
            y = location.Y;

            Field leftMiddleField = EditorToolManager.GetSingleField(x, y);

            if (leftMiddleField != null) {
                if (leftMiddleField.TileField.GetTile(TileFields.MiddleTile) != choosenTile ) {
                    if (leftMiddleField.TileField.GetTile(TileFields.LeftBottomTile) == choosenTile.ChildTileField.GetTile(TileFields.LeftBottomTile)) {
                        _paintTiles.Add(EditorToolManager.SetSingleTile(x, y, null, TileFields.LeftBottomTile));
                    }
                    if (leftMiddleField.TileField.GetTile(TileFields.LeftTopTile) == choosenTile.ChildTileField.GetTile(TileFields.LeftTopTile)) {
                        _paintTiles.Add(EditorToolManager.SetSingleTile(x, y, null, TileFields.LeftTopTile));
                    }
                    _paintTiles.Add(EditorToolManager.SetSingleTile(x, y, choosenTile.ChildTileField.GetTile(TileFields.LeftMiddleTile), TileFields.LeftMiddleTile));
                }
            }
            
            // RightMiddleTile
            x = location.X + 1;
            y = location.Y;

            Field rightMiddleField = EditorToolManager.GetSingleField(x, y);

            if (rightMiddleField != null) {
                if (rightMiddleField.TileField.GetTile(TileFields.MiddleTile) != choosenTile ) {
                    if (rightMiddleField.TileField.GetTile(TileFields.RightBottomTile) == choosenTile.ChildTileField.GetTile(TileFields.RightBottomTile)) {
                        _paintTiles.Add(EditorToolManager.SetSingleTile(x, y, null, TileFields.RightBottomTile));
                    }
                    if (rightMiddleField.TileField.GetTile(TileFields.RightTopTile) == choosenTile.ChildTileField.GetTile(TileFields.RightTopTile)) {
                        _paintTiles.Add(EditorToolManager.SetSingleTile(x, y, null, TileFields.RightTopTile));
                    }
                    _paintTiles.Add(EditorToolManager.SetSingleTile(x, y, choosenTile.ChildTileField.GetTile(TileFields.RightMiddleTile), TileFields.RightMiddleTile));
                }
            }
            
            SetRightTopTile(location.X, location.Y, choosenTile);
            SetLeftTopTile(location.X, location.Y, choosenTile);

            return _paintTiles.ToArray();
        }

        private void SetTopTile(int x, int y, Tile choosenTile) {

            y = y - 1;

            Field field = EditorToolManager.GetSingleField(x, y);
            Field topField = EditorToolManager.GetSingleField(x, y - 1);

            if (field == null) {
                return;
            }

            if (field.TileField.GetTile(TileFields.MiddleTile) == choosenTile) {
                return;
            }
            
            if (topField != null) {
                if (topField.TileField.GetTile(TileFields.MiddleTile) == choosenTile) {
                    _paintTiles.Add(EditorToolManager.SetSingleTile(x, y, choosenTile));
                    _paintTiles.Add(EditorToolManager.SetSingleTile(x, y - 1, choosenTile));
                    return;
                }
            }
            
            if (field.TileField.GetTile(TileFields.LeftTopTile) == choosenTile.ChildTileField.GetTile(TileFields.LeftTopTile)) {
                _paintTiles.Add(EditorToolManager.SetSingleTile(x, y, null, TileFields.LeftTopTile));
            }
            if (field.TileField.GetTile(TileFields.RightTopTile) == choosenTile.ChildTileField.GetTile(TileFields.RightTopTile)) {
                _paintTiles.Add(EditorToolManager.SetSingleTile(x, y, null, TileFields.RightTopTile));
            }
            _paintTiles.Add(EditorToolManager.SetSingleTile(x, y, choosenTile.ChildTileField.GetTile(TileFields.TopTile), TileFields.TopTile));
        }

        private void SetLeftTopTile(int x, int y, Tile choosenTile) {

            x = x - 1;
            y = y - 1;

            Field field = EditorToolManager.GetSingleField(x, y);
            Field topField = EditorToolManager.GetSingleField(x, y - 1);
            Field rightField = EditorToolManager.GetSingleField(x + 1, y);

            if (field == null) {
                return;
            }
            
            if (topField != null) {

                if (topField.TileField.GetTile(TileFields.MiddleTile) == choosenTile || topField.TileField.GetTile(TileFields.TopTile) == choosenTile.ChildTileField.GetTile(TileFields.TopTile)) {
                    if (rightField != null) {
                        if (rightField.TileField.GetTile(TileFields.MiddleTile) != choosenTile) {
                            _paintTiles.Add(EditorToolManager.SetSingleTile(x + 1, y, choosenTile.ChildTileField.GetTile(TileFields.RightMiddleTile), TileFields.RightMiddleTile));
                        }
                    }
                    
                    if (field.TileField.GetTile(TileFields.LeftBottomTile) == choosenTile.ChildTileField.GetTile(TileFields.LeftBottomTile)) {
                        _paintTiles.Add(EditorToolManager.SetSingleTile(x, y, choosenTile.ChildTileField.GetTile(TileFields.LeftMiddleTile), TileFields.LeftMiddleTile));
                    }

                    if (field.TileField.GetTile(TileFields.BottomTile) == choosenTile.ChildTileField.GetTile(TileFields.BottomTile)) {
                        _paintTiles.Add(EditorToolManager.SetSingleTile(x, y, choosenTile.ChildTileField.GetTile(TileFields.LeftMiddleTile), TileFields.LeftMiddleTile));
                    }
                    return;
                }
                
                if (topField.TileField.GetTile(TileFields.LeftMiddleTile) == choosenTile.ChildTileField.GetTile(TileFields.LeftMiddleTile)) {
                    _paintTiles.Add(EditorToolManager.SetSingleTile(x, y, choosenTile.ChildTileField.GetTile(TileFields.LeftMiddleTile), TileFields.LeftMiddleTile));
                    return;
                }
                
                if (topField.TileField.GetTile(TileFields.LeftTopTile) == choosenTile.ChildTileField.GetTile(TileFields.LeftTopTile)) {
                    _paintTiles.Add(EditorToolManager.SetSingleTile(x, y, choosenTile.ChildTileField.GetTile(TileFields.LeftMiddleTile), TileFields.LeftMiddleTile));
                    return;
                }
                
            }
            
            if (field.TileField.GetTile(TileFields.TopTile) == choosenTile.ChildTileField.GetTile(TileFields.TopTile)) {
                return;
            }

            if (field.TileField.GetTile(TileFields.LeftMiddleTile) == choosenTile.ChildTileField.GetTile(TileFields.LeftMiddleTile)) {
                return;
            }

            _paintTiles.Add(EditorToolManager.SetSingleTile(x, y, choosenTile.ChildTileField.GetTile(TileFields.LeftTopTile), TileFields.LeftTopTile));

        }

        private void SetRightTopTile(int x, int y, Tile choosenTile) {

            x = x + 1;
            y = y - 1;

            Field field = EditorToolManager.GetSingleField(x, y);
            Field topField = EditorToolManager.GetSingleField(x, y - 1);
            Field leftField = EditorToolManager.GetSingleField(x - 1, y);

            if (field == null) {
                return;
            }
            
            if (topField != null) {

                if (topField.TileField.GetTile(TileFields.MiddleTile) == choosenTile || topField.TileField.GetTile(TileFields.TopTile) == choosenTile.ChildTileField.GetTile(TileFields.TopTile)) {
                    if (leftField != null) {
                        if (leftField.TileField.GetTile(TileFields.MiddleTile) != choosenTile) {
                            _paintTiles.Add(EditorToolManager.SetSingleTile(x - 1, y, choosenTile.ChildTileField.GetTile(TileFields.LeftMiddleTile), TileFields.LeftMiddleTile));
                        }
                    }
                    
                    if (field.TileField.GetTile(TileFields.RightBottomTile) == choosenTile.ChildTileField.GetTile(TileFields.RightBottomTile)) {
                        _paintTiles.Add(EditorToolManager.SetSingleTile(x, y, choosenTile.ChildTileField.GetTile(TileFields.RightMiddleTile), TileFields.RightMiddleTile));
                    }

                    if (field.TileField.GetTile(TileFields.BottomTile) == choosenTile.ChildTileField.GetTile(TileFields.BottomTile)) {
                        _paintTiles.Add(EditorToolManager.SetSingleTile(x, y, choosenTile.ChildTileField.GetTile(TileFields.RightMiddleTile), TileFields.RightMiddleTile));
                    }
                    return;
                }
                
                if (topField.TileField.GetTile(TileFields.RightMiddleTile) == choosenTile.ChildTileField.GetTile(TileFields.RightMiddleTile)) {
                    _paintTiles.Add(EditorToolManager.SetSingleTile(x, y, choosenTile.ChildTileField.GetTile(TileFields.RightMiddleTile), TileFields.RightMiddleTile));
                    return;
                }
                
                if (topField.TileField.GetTile(TileFields.RightTopTile) == choosenTile.ChildTileField.GetTile(TileFields.RightTopTile)) {
                    _paintTiles.Add(EditorToolManager.SetSingleTile(x, y, choosenTile.ChildTileField.GetTile(TileFields.RightMiddleTile), TileFields.RightMiddleTile));
                    return;
                }
                
            }
            
            if (field.TileField.GetTile(TileFields.TopTile) == choosenTile.ChildTileField.GetTile(TileFields.TopTile)) {
                return;
            }

            if (field.TileField.GetTile(TileFields.RightMiddleTile) == choosenTile.ChildTileField.GetTile(TileFields.RightMiddleTile)) {
                return;
            }

            _paintTiles.Add(EditorToolManager.SetSingleTile(x, y, choosenTile.ChildTileField.GetTile(TileFields.RightTopTile), TileFields.RightTopTile));

        }

    }
}
